constructive algorithms *1300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define str string
#define ld long double
#define run_vasya_run ios_base::sync_with_stdio(0) , cin.tie(0) ,cout.tie(0)
#define ull unsigned long long
using namespace std;
const ll mod = 1000000007;
ll _ , n ,a[300000] , c[300000] , b[300000] , ans , pos;
map<ll,ll>used;
vector<ll>v , v1;
ll binpow(ll a , ll n , ll mod)
{
    if(n == 0)
    {
        return 1ll;
    }
    if(n % 2 == 1)
    {
        return a * binpow(a , n - 1 , mod) % mod;
    }
    return (binpow(a , n / 2 , mod) * binpow(a , n / 2 , mod)) % mod;
}
int main()
{
    run_vasya_run;
    _ = 1;
    while(_ --)
    {
        cin >> n;
        for(int j = 1;j <= n;j ++)
        {
            cin >> a[j];
        }
        for(int j = 1;j <= n;j ++)
        {
            cin >> b[j];
            if(a[j] != b[j])
            {
                ans ++;
            }
            else
            {
                c[j] = a[j];
                used[a[j]] ++;
            }
        }
        if(ans == 1){
            for(int j = 1;j <= n;j ++)
            {
                if(a[j] != b[j])
                {
                    used[a[j]] ++;
                    used[b[j]] ++;
                    pos = j;
                }
            }
            for(int i = 1;i <= n;i ++)
            {
                if(used[i] == 0)
                {
                    c[pos] = i;
                }
            }
        }
        else
        {
            ans = 1;
            for(int j = 1;j <= n;++ j)
            {
                if(used[j] == 0)
                {
                    v.push_back(j);
                }
            }
            ans = 1;
            for(int i = 1;i <= n;++ i)
            {
                if(a[i] != b[i])
                {
                    v1.push_back(i);
                }
            }
            c[v1[0]] = v[0];
            c[v1[1]] = v[1];
            if((a[v1[0]] != v[0]) + (a[v1[1]] != v[1]) == 1 && (b[v1[0]] != v[0]) + (b[v1[1]] != v[1]) == 1)
            {

            }
            else
            {
                swap(c[v1[0]] , c[v1[1]]);
            }
        }
        for(int j = 1;j <= n;j ++)
        {
            cout << c[j] << ' ';
        }
    }
}


Comments

Submit
0 Comments
More Questions

479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game